﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Synchros
{
    public partial class FrmLogger : Form
    {
        public FrmLogger()
        {
            InitializeComponent();
        }

        private void FrmViewLog_Load(object sender, EventArgs e)
        {
            dateTimePicker.Value = DateTime.Now;
            ReadLogFile(DateTime.Now);
        }

        // dataTime format(yyyy-MM-dd)
        private bool ReadLogFile(DateTime dateTime)
        {
            lbLog.Items.Clear();

            // 读取日志文件
            string fileFullName = LogHelper.GetPath(dateTime);
            if (File.Exists(fileFullName))
            {
                // exists or not
                FileStream fs = new FileStream(fileFullName, FileMode.Open, FileAccess.Read);
                StreamReader m_streamReader = new StreamReader(fs);
                // reader files
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                // reader files until end
                string strLine = m_streamReader.ReadLine();
                while (strLine != null)
                {
                    LbLogBind(strLine);
                    strLine = m_streamReader.ReadLine();
                }
                // close StreamReader
                m_streamReader.Close();
                return true;
            }
            else
            {
                return false;
            }
        }

        // 查看日志
        private void btnYes_Click(object sender, EventArgs e)
        {
            if(!ReadLogFile(dateTimePicker.Value))
            {
                MessageBox.Show("此天没有记录日志文件或此天的日志文件已被删除。"); ;
            }
        }

        // 关闭
        private void btnNo_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.No;
        }

        // 日志数据绑定
        private void LbLogBind(string Content)
        {
            if (this.lbLog.InvokeRequired)
            {
                BeginInvoke(new RichTextBoxBindCallBack(LbLogBind), new string[] { Content });
            }
            else
            {
                lbLog.Items.Add(Content);
            }
        }

        private delegate void RichTextBoxBindCallBack(string Content);

        // 删除日志
        private void btnDelete_Click(object sender, EventArgs e)
        {
            string fileName = LogHelper.GetPath(dateTimePicker.Value);
            if (File.Exists(fileName))
            {
                System.Windows.Forms.DialogResult result = MessageBox.Show(
                    "你确定要删除[" + dateTimePicker.Text + "]这天的日志吗？", "", MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    File.Delete(fileName);
                    lbLog.Items.Clear();
                }
            }
            else
            {
                MessageBox.Show("此天没有记录日志文件或此天的日志文件已被删除。");
            }
        }
    }
}
